use -- the proper database below according to the current environment
    Events_Temp 
 -- Test_Events_Temp
 -- Live_Events_Temp

GO
/****** Object:  StoredProcedure [dbo].[spGetEventIndicatorCommentsByCompanyIds]    Script Date: 12/27/2017 6:26:59 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[spGetEventIndicatorOutlookCommentsByCompanyIds]
	@CompanyIds NVARCHAR(MAX)
AS
BEGIN
	SET NOCOUNT ON;

	DECLARE @XML XML
	SET @XML = CONVERT(XML, @CompanyIds, 1)

	DECLARE @SelectedCompanies TABLE
	(
		Id uniqueidentifier
	)
	INSERT INTO @SelectedCompanies
	SELECT TID.id.value('.','uniqueidentifier') AS Id
	FROM @XML.nodes('/CompanyIds') AS T(c) outer apply T.c.nodes('Id') as TID(id)

	SELECT PublishedEventIndicatorComments.[CommentId]
		  ,PublishedEventIndicatorComments.[Comment]
	FROM [dbo].[PublishedEventIndicatorComments] AS PublishedEventIndicatorComments
	INNER JOIN @SelectedCompanies AS SelectedCompanies ON SelectedCompanies.Id = PublishedEventIndicatorComments.CompanyId
	INNER JOIN [dbo].[PublishedEventIndicators] AS PublishedEventIndicators ON PublishedEventIndicatorComments.CommentId = PublishedEventIndicators.OutlookCommentsId
	ORDER BY PublishedEventIndicatorComments.CommentId, PublishedEventIndicatorComments.CommentSortKey
	 
END

GO

